package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.AbandonStudent;
import cn.wolfcode.crm.domain.ClassRoom;
import cn.wolfcode.crm.domain.PayManage;
import cn.wolfcode.crm.mapper.AbandonStudentMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAbandonStudentService;
import cn.wolfcode.crm.service.IPayManageService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AbandonStudentServiceImpl implements IAbandonStudentService {
    @Autowired
    private AbandonStudentMapper abandonStudentMapper;
    @Autowired
    private IPayManageService payManageService;
    public int insert(AbandonStudent record) {

        return abandonStudentMapper.insert(record);
    }

    public int updateByPrimaryKey(AbandonStudent record) {

        return abandonStudentMapper.updateByPrimaryKey(record);
    }


    public List<AbandonStudent> selectAll() {

        return abandonStudentMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int total = abandonStudentMapper.query4Count(qo);
        if(total > 0){
            return new PageResult(total,abandonStudentMapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public int deleteByPrimaryKey(Long id) {

        return abandonStudentMapper.deleteByPrimaryKey(id);
    }

    public void audit(Long abandonStudentId, PayManage payManage) {
        AbandonStudent abandonStudent = abandonStudentMapper.selectByPrimaryKey(abandonStudentId);
        abandonStudent.setStatus(true);
        abandonStudentMapper.updateByPrimaryKey(abandonStudent);
        payManage.setBill_number(String.valueOf(new Date().getTime()));
        payManage.setTime(new Date());
        payManage.setState(false);
        ClassRoom classMan = abandonStudent.getFormalStudent().getIntentionClass();
        payManage.setClassMan(classMan);
        payManage.setMoney(abandonStudent.getRefundAmount());
        payManageService.insert(payManage);
    }
}
